﻿using AutoMapper;
using B.R.RabbitRoot.Domain;
using B.R.RabbitRoot.ErrorCode;
using B.R.RabbitRoot.Infrastructure.Interface;
using B.R.RabbitRoot.Write.API.Applications.Commands.SourceCommands;
using MediatR;
using System.Linq;

namespace B.R.RabbitRoot.Write.API.Applications.Commandhandlers.SourceCommandhandelrs
{
    /// <summary>
    /// 溯源删除命令处理
    /// </summary>
    public class SourceDeleteCommandHandler : IRequestHandler<SourceDeleteCommand, APIResult<int>>
    {
        private readonly IBaserepository<SourceInfo> sourceInfoRepsitoty;
        private readonly IMapper mapper;

        public SourceDeleteCommandHandler(IBaserepository<SourceInfo> sourceInfoRepsitoty, IMapper mapper)
        {
            this.sourceInfoRepsitoty = sourceInfoRepsitoty;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(SourceDeleteCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result=new APIResult<int>();
            result.Code=ResultCode.删除成功;
            result.Message = ResultCode.删除成功.ToString();
           var list=sourceInfoRepsitoty.GetAll().FirstOrDefault(x=>x.SourceInfoId==request.SourceInfoId);
            var entity= mapper.Map<SourceInfo>(list);
            entity.DeleteIs = true;
            await sourceInfoRepsitoty.UpdateAsync(entity);
            
            return await Task.FromResult(result);
        }
    }
}
